/*loopGame.js
*/

function framePong(game){
	this.name;
	this.enemy;
	this.socket = game.socket;
	this.context = game.context;
	this.loopMode = 'a';//server or client
	this.loopCount = 0;
	this.width = window.innerWidth;
	this.height = window.innerHeight;
	this.entities = new Array();
		this.entities[0] = new player(this);
		this.entities[1] = new enemy(this);
		this.entities[2] = new ball(this);
		this.entities[3] = new wall(this);
		this.entities[4] = new wall(this);
	this.event = new eventController();
	
	var loop;
	var numbers = new nr(this.context);
	var loopStop = false;
	//var filterStrength = 20;
	//var frameTime = 0, lastLoop = new Date, thisLoop;
	var self = this;

	
	
	
	
	this.mainLoop = function(){
		var startTime = new Date();
		if(this.loopMode != 'buffering') this.loopCount++;
		this.event.fire('onPreTick', 'test');
		$(this.entities).each(function(key, entiti){
			//if(this.loopMode == 'server')
			if(self.loopMode == 'server') entiti.serverTick();
			else entiti.clientTick(); //on server and buffering
		});
		this.event.fire('onPostTick', 'test');
		game.context.clearRect(0, 0, this.width, this.height);
		$(this.entities).each(function(key, entiti){
			game.context.fillRect(entiti.x-entiti.width/2, entiti.y-entiti.height/2, entiti.width, entiti.height);
		});
		//rander ui
		//mittellinie
		game.context.fillRect((this.width/2-5), 0, 10, this.height);
		//points
		numbers.draw(this.entities[0].points, (this.width/2-120), 50);
		numbers.draw(this.entities[1].points, (this.width/2+60), 50);
		//next loop
		if(loopStop){
			loopStop = false;
		}else{
			var loopTime = (new Date()-startTime);
			loop = setTimeout(function(){self.mainLoop();}, (50-loopTime));
			//lastLoop = thisLoop;
		}
	};
	this.exit = function(){
		game.changeRunningFrame('shell', {
			action : 'gameend',
			player : this.entities[0].points,
			enemy : this.entities[1].points,
		});
	}
	this.start = function(options){
		game.context.clearRect(0, 0, game.width, game.height);
		if(options.resolution[0]<this.width){
			this.width = options.resolution[0];
		}
		if(options.resolution[1]<this.height){
			this.height = options.resolution[1];
		}
		game.resize(this.width, this.height);
		
		this.enemy = options.enemy;
		this.name = game.name;
		
		this.entities[0].x = 20;
		this.entities[0].y = (this.height/2);

		this.entities[1].x = (this.width-20);
		this.entities[1].y = (this.height/2);

		this.entities[2].x = (this.width/2);
		this.entities[2].y = (this.height/2);
		this.entities[2].dx = 0;
		this.entities[2].dy = 0;

		this.entities[3].x = this.width/2;
		this.entities[3].y = -40;
		this.entities[3].width = this.width;

		this.entities[4].x = this.width/2;
		this.entities[4].y = (this.height + 40);
		this.entities[4].width = this.width;
		
		bindKeys();
		this.socket.attach(this.entities[1], 'player');
		this.socket.attach(this.entities[2], new Array('ball', 'pickup', 'goal') );
		loopStop = false;
		this.entities[2].startRound(options.loopMode);
		this.mainLoop();

	};
	
	this.stop = function(){
		loopStop = true;
		clearTimeout(loop);
		//$(document).unbind(keydown);
	//	$(document).unbind(click);
	};
	this.unsetDrawObject = function(obj){
		for(var i=0;i<this.entities.length;i++){
			if(this.entities[i] == obj){
				this.entities.splice(i, 1);
			}
		}
	}
	this.setDrawObject = function(obj){
		this.entities.push(obj);
	}
	
	//set bilndings
	function bindKeys(){
		$(document).keydown(function(e) {
			if(e.keyCode == 38){
				self.entities[0].setDirection(0, -1);
			}
			if(e.keyCode == 40){
				self.entities[0].setDirection(0, 1);
			}
		});
		$(document).keyup(function(e) {
			if(e.keyCode == 38){
				self.entities[0].setDirection(0, 0);
			}
			if(e.keyCode == 40){
				self.entities[0].setDirection(0, 0);
			}
		});
	}
	this.addPickup = function(data){
		if(data){
			var x = this.width-data[1];
			var code = 'new '+data[0]+'(this, this.enemy, x, '+data[2]+')';
		}else{
			var types = new Array('pickup_speedup');
			var rad = 0;
			var x = (this.width/2)+(Math.random()*(this.width/2-120)+20);
			var y = Math.random()*(this.height/2-40)+20;
			var code = 'new '+types[rad]+'(this, this.name, x, y)';
			game.socket.send('pickup', this.enemy, new Array(types[rad],x,y));
		}

		this.entities.push(eval(code) );
		
	}
}
//server legt pickups
//-> send to enemy
function pickup_speedup(pong, o, x, y){
	this.instaceof = 'pickup';
	this.owner = o;
	this.x = x;
	this.y = y;
	this.width = 20;
	this.height = 20;
	var self = this;
	
	this.stop = function(){
		pong.entities[2].speed = pong.entities[2].speed/2;
		pong.event.detach(self);
	//kill this stop it !!!!
	};
	this.start = function(){
		console.log(this.owner+" != "+pong.name);
		if(this.owner != pong.name && pong.entities[2].dx <0){
			pong.event.attach(this);
			pong.unsetDrawObject(this);
			pong.entities[2].speed = pong.entities[2].speed*2;
		}
	};
	
	this.serverTick = function(){ 
	};
	this.clientTick = function(){ 
	};
	this.onPreTick = function(data){
	};
	this.onPostTick = function(data){
	};
	this.onColl = function(data){
//		data[0] = ball
//		data[1] = target
		if(data[1] instanceof player){
			this.stop();
		}
		
	};
	this.onChangeMode = function(){
		//server or client
	};
	this.goal = function(){
		this.stop();
	};
}
function pickup_testr(pong, o){
	this.instaceof = 'pickup';
	this.owner = o;
	this.x = 0;
	this.y = 0;
	this.width = 20;
	this.height = 100;
	
	this.stop = function(){
		pong.event.detach(self);
	};
	this.start = function(){
		pong.event.attach(this);
		pong.unsetDrawObject(this);
	};
	
	this.tick = function(){ 
	};
	
	this.onPreTick = function(data){
		
	};
	this.onPostTick = function(data){
		
	};
	this.onColl = function(data){
//		data[0] = ball
//		data[1] = target
	};
	this.onChangeMode = function(){
		//server or client
	};
	this.goal = function(){
		this.stop();
	};
}
function eventController(){
	var listners = new Array();
	
	this.fire = function(name, data){
		$(listners).each(function(key,value){
			var func = 'value.'+name;
			try{
				eval(func)(data);
			}catch (e) {
				// TODO: handle exception
			}
		});
	};

	this.attach = function(listner){
		listners.push(listner);
	};
	this.detach = function(listner){
		var i;
		for(i=0;i<listners.length;i++){
			if(listners[i] == listner){
				listners.splice(i, 1);
				return false;
			}
		}
	};

}


